<template>
  <div id="container">
    <el-form ref="form" :model="student" label-width="120px" :rules="rules">
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="student.name" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="student.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年级" prop="sex">
        <el-radio-group v-model="student.class">
          <el-radio v-for="item in classList" :key="item.id" :label="item.id">{{
            item.name
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入学时间" prop="create_time">
        <el-date-picker
          v-model="student.create_time"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="time"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-folder-checked" type="primary" @click="save">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import studentApi from "@/api/student";
import { mapState } from "vuex";

export default {
  name: "AddStudent",
  data() {
    return {
      student: {
        avatar: "",
        name: "",
        sex: "男",
        class: 1,
        create_time: "",
      },
      // 验证规则
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        create_time: [
          { required: true, message: "请选择日期", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapState("classL", ["classList"]),
  },
  mounted() {
    if (this.$route.params.id) {
      this.getStudentInfo();
    }
  },
  methods: {
    // 获取学生信息
    getStudentInfo() {
      studentApi.getStudentInfo(this.$route.params.id).then((res) => {
        this.student = res.data;
      });
      // 对时间进行格式化
      this.student.create_time = new Date(this.student.create_time);
    },
    // 保存按钮
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.student.id ? this.update() : this.add();
        } else {
          this.$message.warning("请检查表单信息");
        }
      });
    },
    // 添加学生
    add() {
      studentApi
        .addStudent(this.student)
        .then((res) => {
          if (res.code === 20000) {
            this.$message.success(res.msg);
            this.$router.push("/student/list");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 更新学生
    update() {
      studentApi
        .updateStudent(this.student)
        .then((res) => {
          if (res.code === 20000) {
            this.$message.success(res.msg);
            this.$router.push("/student/list");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style scoped>
#container {
  padding: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/*el-input的长度*/
.el-input {
  width: 400px;
}
</style>
